﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using OpenQuant;
using QuantBox;
using Skyline;
using SmartQuant;

namespace CtpseDemo
{
    public class DemoScenario : Scenario
    {
        public DemoScenario(Framework framework) : base(framework)
        {
        }

        private void UpdateInstrument()
        {
            var list = new List<Instrument>();
            //更新上市的期货和期权合约
            list.AddRange(InstrumentManager.GetLiveFutures(DateTime.Today));
            list.AddRange(InstrumentManager.GetLiveFutureOptions(DateTime.Today));
            if (list.Count > 0)
            {
                InstrumentManager.Clear();
                foreach (var item in list)
                {
                    if (Regex.IsMatch(item.Symbol, @"[a-zA-Z]+[89]+"))
                    {
                        continue;
                    }
                    InstrumentManager.Add(item);
                }
                InstrumentManager.Server.Flush();
            }
        }

        public override void Run()
        {
            StrategyManager.Mode = StrategyMode.Live;
            InstrumentManager.Load();
            if (InstrumentManager.Instruments.Count == 0)
            {
                UpdateInstrument();
            }

            strategy = new Strategy(framework, "CtpseTest");

            var strategyTypes = typeof(DemoScenario).Assembly
                .GetTypes()
                .Where(n => n.IsSubclassOf(typeof(Strategy)))
                .ToDictionary(n => n.Name, n => n);
            foreach (var key in strategyTypes.Keys)
            {
                Console.WriteLine(key);
            }

            //foreach (var strategyName in new[] { "TradingTestStrategy" })
            //{
            //    if (strategyTypes.TryGetValue(strategyName, out var type))
            //    {
            //        var strategy = (Strategy)Activator.CreateInstance(type, framework, strategyName);
            //        //strategy.AddInstrument();
            //    }
            //}

            var strategy1 = new EtfTestStrategy(framework, "test");
            strategy.AddStrategy(strategy1);

            var provider = ProviderManager.NewCtpData("银河电信", "", "");
            provider.DiscardEmptyTrade = false;
            provider.QueryTradingDataAfterTrade = false;
            provider.MaxTimeDiffExchangeLocal = 0;
            provider.DiscardOutOfTimeRange = false;

            foreach (var instrument in InstrumentManager.Instruments)
            {
                if (instrument.Type == InstrumentType.FutureOption || instrument.Exchange != "SHFE")
                {
                    continue;
                }

                //Cfg.LimFutPosOpen[instrument.Symbol] = 1330;
                instrument.DataProvider = provider;
                instrument.ExecutionProvider = provider;
                strategy1.AddInstrument(instrument);
            }

            //OrderRegister.Instance.Init(framework);
            var server = new StrategyServer(this).Init(@"c:\work\data\CtpseTest.db");

            if (OpenQuantOutside.RunInOpenQuant)
            {
                StartLive();
            }
            else
            {
                framework.StrategyManager.StartStrategy(strategy, StrategyMode.Live);
                CommandLineApp.Run();
                foreach (var instrument in strategy.Instruments)
                {
                    instrument.ExecutionProvider?.Disconnect();
                }
                framework.StrategyManager.Stop();
                framework.EventManager.Stop();
            }
            server.Release();
        }
    }
}